package com.new2i.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView;

import com.new2i.aop.LoginInterceptor;
import com.new2i.service.able.ApiService;
import com.new2i.service.able.UserService; 
import com.weixin.util.WeixinHttpUtil; 

/**
 * 根目录视图控制器
 * 
 * @since 2015/08/25
 * @author 2i
 *
 */
@Controller  
public class IndexAction extends BaseAction{
	
	@Autowired
	private transient UserService mUserService;
	
	@Autowired
	private transient ApiService mApiService;
	
	
	private static Logger logger = Logger.getLogger(IndexAction.class);
	
	/**
	 * 首页页面
	 * 
	 * @author 2i
	 * @since 2015/09/01
	 * @param request
	 * @param response
	 * @return
	 */
	@LoginInterceptor
	@RequestMapping({"","/index"})
	public ModelAndView indexPage(HttpServletRequest request,HttpServletResponse response) { 
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/views/main.jsp");
		return mav; 
	}
	
	/**
	 * 首页页面
	 * 
	 * @author 2i
	 * @since 2015/09/01
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/api.action")
	public ModelAndView mePage(HttpServletRequest request,HttpServletResponse response,String code) {  
		ModelAndView mav = new ModelAndView(); 
		HashMap<String, Object> userInfo = null; 
		logger.info("code==>" + code);  
		Object oWxid = request.getSession().getAttribute("wxid"); 
		if((oWxid == null || oWxid.toString().length() <= 0)) { 
			//session中无值，从微信的授权中获取微信open_id   
			String wxid = WeixinHttpUtil.getInstance().getWxidByOauth2Code(code);
			userInfo = mUserService.getUserInfoByWxCode(wxid);  
			String sUserId =  userInfo.get("user_id").toString(); 
			request.getSession().setAttribute("user_id", sUserId); 
			request.getSession().setAttribute("wxid", wxid);
		}else{
			String wxid = oWxid.toString();
			userInfo = mUserService.getUserInfoByWxCode(wxid);
		}    
		logger.info("userInfo==>" + userInfo);
		mav.setViewName("/views/me.jsp");
		mav.addObject("me", userInfo);
		return mav; 
	}
	
	/**
	 * 首页页面
	 * 
	 * @author 2i
	 * @since 2015/09/01
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/api/list.action")
	public ModelAndView apiListPage(HttpServletRequest request,HttpServletResponse response) {  
		ModelAndView mav = new ModelAndView();   
		List<String> list = mApiService.selectApiList();
		mav.addObject("api_home",list);
		mav.setViewName("/api_home.jsp");  
		return mav;
	}
	
	/**
	 * 首页页面
	 * 
	 * @author 2i
	 * @since 2015/09/01
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/api/info.action")
	public ModelAndView apiInfoPage(HttpServletRequest request,HttpServletResponse response,String api_path) {  
		ModelAndView mav = new ModelAndView();   
		List<?> list = mApiService.getApiInfo(api_path);
		mav.addObject("api_info",list);
		mav.setViewName("/api_info.jsp");  
		return mav;
	}
	 
}
